﻿using System;

namespace pk3DS.Core.Structures;

public class EncounterStatic6(byte[] data)
{
    // All
    public readonly byte[] Data = (byte[])data.Clone();

    public ushort Species
    {
        get => BitConverter.ToUInt16(Data, 0x0);
        set => BitConverter.GetBytes(value).CopyTo(Data, 0x0);
    }

    public byte Form
    {
        get => Data[0x2];
        set => Data[0x2] = value;
    }

    public byte Level
    {
        get => Data[0x3];
        set => Data[0x3] = value;
    }

    public int HeldItem
    {
        get
        {
            int item = BitConverter.ToInt16(Data, 0x4);
            if (item < 0) item = 0;
            return item;
        }
        set
        {
            if (value == 0) value = -1;
            BitConverter.GetBytes((short)value).CopyTo(Data, 0x4);
        }
    }

    public bool Shiny
    {
        get => (Data[0x6] & 1) >> 1 == 1;
        set => Data[0x6] = (byte)((Data[0x6] & ~1) | (value ? 1 : 0));
    }

    public bool ShinyLock
    {
        get => (Data[0x6] & 2) >> 1 == 1;
        set => Data[0x6] = (byte)((Data[0x6] & ~2) | (value ? 2 : 0));
    }

    public int Gender
    {
        get => (Data[0x6] & 0xC) >> 2;
        set => Data[0x6] = (byte)((Data[0x6] & ~0xC) | ((value & 3) << 2));
    }

    public int Ability
    {
        get => (Data[0x6] & 0x70) >> 4;
        set => Data[0x6] = (byte)((Data[0x6] & ~0x70) | ((value & 7) << 4));
    }

    public bool IV3
    {
        get => (Data[0x7] & 1) >> 0 == 1;
        set => Data[0x7] = (byte)((Data[0x7] & ~1) | (value ? 1 : 0));
    }

    public bool IV3_1
    {
        get => (Data[0x7] & 2) >> 1 == 1;
        set => Data[0x7] = (byte)((Data[0x7] & ~2) | (value ? 2 : 0));
    }

    public byte[] Write() => (byte[])Data.Clone();
}